package org.linlinjava.litemall.admin.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.util.PageUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "订阅用户日志服务")
@RestController
@RequestMapping("/admin/lumiere/subscribeUserLog")
@Validated
@RequiredArgsConstructor
public class AdminLumiereSubscribeUserLogController {

    /**
     * 卢米埃订阅用户日志服务
     */
    private final LumiereSubscribeUserLogService lumiereSubscribeUserLogService;

    /**
     * 卢米埃订阅服务
     */
    private final LumiereSubscribeService lumiereSubscribeService;

    private final LumiereSubscribeUserOrderService lumiereSubscribeUserOrderService;

    /**
     *
     */
    private final SubscribeUserLogService subscribeUserLogService;

    private final LitemallOrderService litemallOrderService;

    /**
     * 订阅记录列表
     *
     * @param payType       支付类型
     * @param userName      用户名
     * @param subscribeName 订阅名字
     * @param page          页面
     * @param limit         限制
     * @param sort          排序
     * @param order         订单
     * @return {@link Object}
     */
    @ApiOperation(value = "订阅记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payType",value = "支付类型"),
            @ApiImplicitParam(name = "userName",value = "用户名"),
            @ApiImplicitParam(name = "page",value = "页"),
            @ApiImplicitParam(name = "limit",value = "数"),
    })
    @GetMapping("/list")
    @LogAnno
    public Object list(Integer payType, String userName, String subscribeName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        if(subscribeName == "" ){
            subscribeName = null;
        }
        List<LumiereSubscribeUserLog> ll = subscribeUserLogService.getUserLogByCondition(payType, userName, subscribeName, page, limit);
        Pageable pageable = PageRequest.of(page -1 ,limit);
        return ResponseUtil.ok(PageUtil.getPageLumierePayMemberUserLogVo(pageable,ll));
    }

    /**
     * 用户订阅记录列表
     *
     * @param userId  用户id
     * @param page    页面
     * @param limit   限制
     * @return {@link Object}
     */
    @ApiOperation(value = "用户订阅记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID"),
            @ApiImplicitParam(name = "page",value = "页"),
            @ApiImplicitParam(name = "limit",value = "数")
    })
    @GetMapping(value = "/listById")
    @LogAnno
    public ResponseUtil.Response listById( Integer userId,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit){
        List<LumiereSubscribeUserLog> lumiereSubscribeUserLogIPage = lumiereSubscribeUserLogService.selectJoinList(LumiereSubscribeUserLog.class, new MPJLambdaWrapper<LumiereSubscribeUserLog>()
                .selectAll(LumiereSubscribeUserLog.class)
                .leftJoin(LitemallUser.class, LitemallUser::getId, LumiereSubscribeUserLog::getSubscribeId)
                .leftJoin(LumiereSubscribe.class, LumiereSubscribe::getId, LumiereSubscribeUserLog::getUserId)
                .eq(LumiereSubscribeUserLog::getUserId,userId)
                .orderByDesc(LumiereSubscribeUserLog::getAddTime)
        );
        Pageable pageable = PageRequest.of(page -1 ,limit);
        return ResponseUtil.ok(PageUtil.getPageLumierePayMemberUserLogVo(pageable,lumiereSubscribeUserLogIPage));
    }
}
